package com.coldchain.admin.user.api;


import com.coldchain.admin.user.entity.Company;
import com.coldchain.admin.user.entity.CompanyApplyAmount;
import com.coldchain.admin.user.entity.CompanySearch;
import com.coldchain.admin.user.service.ICompanyApplyAmountService;
import com.coldchain.admin.user.service.ICompanyService;
import com.coldchain.authclient.annotation.CheckClientToken;
import com.coldchain.authclient.annotation.CheckUserToken;
import com.coldchain.common.enums.CompanyStatusEnum;
import com.coldchain.common.msg.BaseResponse;
import com.coldchain.common.msg.ObjectRestResponse;
import com.fangliai.core.context.BaseContextHandler;
import com.fangliai.core.entitiy.PageResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 * 企业信息表 前端控制器
 * </p>
 *
 * @author fangliai
 * @since 2018-09-20
 */
@RestController
@RequestMapping("/company")
@CheckUserToken
@CheckClientToken
public class CompanyController {

    @Autowired
    private ICompanyService companyService;


    @Autowired
    private ICompanyApplyAmountService applyAmountService;

    /**
     * 查询不分页面
     * @param search
     * @return
     */
    @PostMapping("/list")
    public ObjectRestResponse<PageResponse<Company>> lsit(CompanySearch search){
        return  new ObjectRestResponse<>().data(companyService.list(search.initWrapper()));
    }

    /**
     * 查询分页
     * @param search
     * @return
     */
    @PostMapping("/page")
    public ObjectRestResponse<PageResponse<Company>> lsitByPage(CompanySearch search){
        return  new ObjectRestResponse<>().data(companyService.search(search));
    }

    /**
     * 编辑
     * @param company
     * @return
     */
    @PostMapping("/edit")
    public BaseResponse edit(Company company)throws Exception{
        if(StringUtils.isEmpty(company.getCompanyId())){
            companyService.register(company);
        }else {
            companyService.saveOrUpdate(company);
        }
         return new ObjectRestResponse<>().succeed();
    }

    /**
     * 删除
     * @param companyId
     * @return
     */
    @RequestMapping("/del")
    public BaseResponse del(String companyId){
        companyService.removeById(companyId);
        return new ObjectRestResponse<>().succeed();
    }

    /**
     * 修改企业状态
     * @param companyId
     * @return
     */
    @RequestMapping("/change")
    public BaseResponse changeStatus(String companyId){
        Company company=companyService.getById(companyId);
        company.setStatus(company.getStatus().equals(CompanyStatusEnum.BLACKLIST)?CompanyStatusEnum.NORMAL:CompanyStatusEnum.BLACKLIST);
        companyService.updateById(company);
        return new ObjectRestResponse<>().succeed("ok");
    }




    /**
     * 申请信用额度
     * @param applyAmount
     * @return
     */
    @RequestMapping("/apply")
    public BaseResponse applyAmount(CompanyApplyAmount applyAmount){
        applyAmount.setCompanyId(BaseContextHandler.getCommpanyId());
        applyAmount.setApplyTime(new Date().getTime());
        applyAmount.setStatus(0);
        return new ObjectRestResponse<>().succeed();
    }



}

